#include"Heap.h"

void HeapTest()
{
    int a[] = {6,1,2,3,5};

    HP hp;
    HPInit(&hp);
    int i = 0;
    for(i = 0; i < sizeof(a)/sizeof(a[0]); i++)
    {
        HPPush(&hp, a[i]);
    }

    HPPrint(&hp);
    HPPop(&hp);
    printf("%d\n", HPTop(&hp));
    printf("%d\n",HPSize(&hp));
    HPPop(&hp);
    HPPop(&hp);
    HPPop(&hp);
    HPPop(&hp);
    printf("%d\n",HPEmpty(&hp));
    HPDeatroy(&hp);
}
void HeapTest1()
{
    
    int a[] = {6,1,2,3,5};

    HP hp;
    HPInit(&hp);
    int i = 0;
    for(i = 0; i < sizeof(a)/sizeof(a[0]); i++)
    {
        HPPush(&hp, a[i]);
    }

  //  while(!HPEmpty(&hp))
  //  {
  //  //    printf("%d ", HPTop(&hp));
  //      
  //      HPPop(&hp);
  //  }
  //
    for(i = 0; i < 5; i++)
    {
        a[i] = HPTop(&hp);
        printf("%d ", a[i]);
        HPPop(&hp);
    }
}
void heapsort(int* a, int n)
{
    //
    int i = 1;
   // for(i = 1; i < n; i++)
   // {
   //     AdjustUP(a, i)
   // }

    for(i = (n - 1 - 1) / 2 ; i >= 0; i--)
    {
        AdjustDown(a, n, i);
    }
   
    int end = n - 1;
    while(end > 0)
    {

        HPSwap(&a[0], &a[end]);
        AdjustDown(a, end, 0);
        end--;
    }


}


void HeapTest3()
{

    int a[] = {6,1,2,3,5,8,9,4};

    HP hp;
    HPInit(&hp);
    heapsort(a, 8);
    int i = 0;
    for(i = 0; i < 8; i++)
    {
        printf("%d ", a[i]);
    }
       
}
void HeapTest4()
{
    int a[] = {3,4,77,88,1,2,9,10,23};
//最好常量都用变量
    int* kin = (int*) malloc(sizeof(int) * 3);
    int i = 0;
    for(i = 0; i < 3; i++)
    {
        kin[i] = a[i];
    }

    for(i = (3-1-1)/2; i >= 0; i--)
    {
        AdjustDown(kin, 3, i);
    }

    for(i = 3; i < 9; i++)
    {
        if(a[i] > kin[0])
        {
            kin[0] = a[i];

            AdjustDown(kin, 3, 0);
        }
    }
    for(i = 0; i<3; i++)
    {
        printf("%d ", kin[i]);
    }

    free(kin);
}
int main()
{
    HeapTest4();
    return 0;
}
